package web.suzy.oj.manager.oj;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.UnicodeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import web.suzy.oj.config.NacosSwitchConfig;
import web.suzy.oj.config.SwitchConfig;
import web.suzy.oj.config.WebConfig;
import web.suzy.oj.dao.common.AnnouncementEntityService;
import web.suzy.oj.dao.common.FileEntityService;
import web.suzy.oj.pojo.entity.common.File;
import web.suzy.oj.pojo.vo.AnnouncementVO;
import web.suzy.oj.utils.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * YangSuzy 软件工作室
 * 类名: HomeManager
 * 描述: 首页功能管理器
 * 功能: ---- 待定 -----
 * =============未完成================
 *
 * @author YangSuzy
 * Date: 2022/11/16 19:00
 */
@Component
public class HomeManager {

    @Autowired
    private NacosSwitchConfig nacosSwitchConfig;

    @Autowired
    private AnnouncementEntityService announcementEntityService;

    @Autowired
    private FileEntityService fileEntityService;

    /**
     * 方法名: getWebConfig
     * 描述: 获取网站的基本信息和配置
     *
     * @return java.util.Map<java.lang.Object, java.lang.Object>
     * @date 2022/11/16 19:01
     * @auther YangSuzy
     **/
    public Map<Object, Object> getWebConfig() {
        SwitchConfig switchConfig = nacosSwitchConfig.getSwitchConfig();
        WebConfig webConfig = nacosSwitchConfig.getWebConfig();
        return MapUtil.builder().put("baseUrl", UnicodeUtil.toString(webConfig.getBaseUrl()))
                .put("name", UnicodeUtil.toString(webConfig.getName()))
                .put("shortName", UnicodeUtil.toString(webConfig.getShortName()))
                .put("register", webConfig.getRegister())
                .put("recordName", UnicodeUtil.toString(webConfig.getRecordName()))
                .put("recordUrl", UnicodeUtil.toString(webConfig.getRecordUrl()))
                .put("description", UnicodeUtil.toString(webConfig.getDescription()))
                .put("email", UnicodeUtil.toString(webConfig.getEmailUsername()))
                .put("projectName", UnicodeUtil.toString(webConfig.getProjectName()))
                .put("projectUrl", UnicodeUtil.toString(webConfig.getProjectUrl()))
                .put("openPublicDiscussion", switchConfig.getOpenPublicDiscussion())
                .map();
    }

    /**
     * 方法名: getCommonAnnouncement
     * 描述: 获取主页公告列表
     *
     * @param limit
     * @param currentPage
     * @return com.baomidou.mybatisplus.core.metadata.IPage<web.suzy.oj.pojo.vo.AnnouncementVO>
     * @date 2022/11/27 11:36
     * @auther YangSuzy
     **/
    public IPage<AnnouncementVO> getCommonAnnouncement(Integer limit, Integer currentPage) {
        //设置默认当前页和每页默认显示数目
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        if (limit == null || limit < 1) {
            limit = 10;
        }
        return announcementEntityService.getAnnouncementList(limit, currentPage, true);
    }

    /**
     * 方法名: getHomeCarousel
     * 描述: 获取主页轮播图
     *
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @date 2022/12/10 18:11
     * @auther YangSuzy
     **/
    public List<HashMap<String, Object>> getHomeCarousel() {
        List<File> fileList = fileEntityService.queryCarouselFileList();
        return fileList.stream().map(f -> {
            HashMap<String, Object> param = new HashMap<>(2);
            param.put("id", f.getId());
            param.put("url", Constants.File.IMG_API.getPath() + f.getName());
            return param;
        }).collect(Collectors.toList());
    }
}
